from product.models import Product
from django.shortcuts import render
from django import forms
from django.http import HttpResponse
# widget=forms.FileInput(attrs={'class':'productInput',})

class InputForm(forms.Form):
	Name = forms.CharField(max_length = 16,widget=forms.TextInput(attrs={'id':'nameInput','class':'productInput'}))
	Thumbnail = forms.ImageField(widget=forms.FileInput(attrs={'id':'thumbnailInput','name':'thumbnail'}))
	Description = forms.CharField(max_length = 64,widget=forms.TextInput(attrs={'id':'descriptionInput','class':'productInput'}))
	OnSaleTime = forms.DateTimeField(widget=forms.TextInput(attrs={'id':'onSaleTime','class':'productInput'}))
	Detail = forms.CharField(max_length = 512,widget=forms.Textarea(attrs={'id':'detailInput','class':'productInput'}))
	


def List(request):
	products = Product.objects.all()
	return render(request, 'list.html', {'products': products})

def Input(request):
	if request.method == "POST":
		ipform = InputForm(request.POST, request.FILES)
		isSuccessful = False

		if ipform.is_valid():
			Name = ipform.cleaned_data['Name']
			thumbnail = ipform.cleaned_data['Thumbnail']
			description = ipform.cleaned_data['Description']
			detail = ipform.cleaned_data['Detail']
			onSaletime = ipform.cleaned_data['OnSaleTime']

			product = Product()
			product.name = Name
			product.thumbnail = thumbnail
			product.detail = detail
			product.description = description
			product.onSaleTime = onSaletime
			try:
				product.save()
				isSuccessful = True
			except:
				print "upload error!"

			return render(request, 'tempInput.html', {'prodcut':product,'isSuccessful':isSuccessful})
	else:
		ipform = InputForm()

	return render(request, 'input.html',{'inputForm':ipform})

def More(request):
	id = request.GET.get('id')
	product = Product.objects.get(id = id)
	return render(request, 'more.html',{'product':product})

def Delete(request):
	isSuccessful = False
	id = request.GET.get('id')
	try:
		product = Product.objects.get(id = id)
		product.delete()
		isSuccessful = True
	except:
		print "delete error"
	return render(request, 'delete.html',{'isSuccessful':isSuccessful})
